package com.luzhikun.aSynchronouse_1;

import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
public class ComputerThread implements Runnable {
    private AsyncContext asyncContext;

    public ComputerThread(AsyncContext asyncContext) {
        this.asyncContext = asyncContext;
    }

    @Override
    public void run() {
        try {
//            PrintWriter writer = asyncContext.getResponse().getWriter();
//            writer.println("子线程开启 <br>");
            // 真正在开发中的使用方式
            HttpServletRequest request = (HttpServletRequest) asyncContext.getRequest();
            HttpSession session = request.getSession();
            int sum=0;
            for (int i = 0; i < 10; i++) {
                Thread.sleep(1000);
                sum+=i;
                System.out.println(sum);
//                writer.println(i + "<br>");
            }
            session.setAttribute("sum",sum);
//            writer.println("子线程结束 <br>");
            // 通知主线程子线程执行完毕
//            asyncContext.complete();
//            asyncContext.dispatch("/index.jsp");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

